/*
h.a = array with all the elements
h.b = Get Blur value
h.c = add Class to the fields
h.d = default options
h.e = bind Events
h.f = Get Focus value
h.g = Get Element
h.o = Get Elment options
h.s = Store Elements (plural, store them all)
h.t = Store one element, with the options and other stuff
h.v = Get original Value
h.w = Write blur value
*/
(function($) {


h = function(o){
//	console.log(this);
//	console.log(o);
	o = $.extend(null,h.d, o);
	
	$.each(this,function(){  
		//console.log(this)
		
		// Store data, the jQuery way.
		$(this).data('o',{
				s	:	$(this).val(),
				o	:	o
			});
		
		// Add the CSS class
		$(this).addClass(o.css);
		// Write the blur value
		$(this).val(h.b(this));
//	});
	

	// Bind events on each and every one of theese little ones.
	//$.each(this,function(){
	//	console.log(this);
		
		// if bound is NOT undefined AND NOT false
		if(this.bound != true){
			// h.o(this.e).bindFocus <- Gets the kind of binding we want.
			//console.log($(this).data('options'));
			
			$(this).bind('focus',function(){
				// If this Value is the same as the "blur" value, Then set the 'focus' value
				// and remove classes.
				if($(this).val() == h.b(this)){
					$(this).val(h.f(this));
					$(this).removeClass(h.o(this).css);
				}

			});
			$(this).bind('blur',function(){
				if($(this).val() == h.f(this)){
					$(this).val(h.b(this));
					$(this).addClass(h.o(this).css);
				}
			});
			this.bound = true;
		}
	});
	
	//console.log(h.a);
	return this;
};

$.fn.hinttoggle = h;


/*
css		The CSS class that is toggled
default: 'hinttoggle'

focusValue	The value that is printed in the input on focus
default: ""

blurValue	The value that is printed in the input field on blur (unfous)
default: null

Both blurValue and focusValue can have the value null which is NOT the same as "" or false.

null means that the value will be taken from what is inside the input field.

*/
h.d = {
css:'hinttoggle'
,focusValue:""
,blurValue:null
//,writeBlurValue:true
};



// Gets the value that should be associated with this element.

h.f = function(v){
	return h.v(v, h.o(v).focusValue);
}

h.b = function(v){
	// Get original value (h.v), get the blurValue from options (h.o(v))
	return h.v(v, h.o(v).blurValue);
}

// If the second parameter is NOT null or undefined this function WILL return that value.
// If the second parameter IS null or undefined the function will return the original value
h.v = function(s, t){
	r = "";
	if(t == null){
		r = $(s).data('o').s
			// Did NOT find the element in the big array.
			// Where can it be? At this point the returnValue is ""
		
	}else{
		// if the elementOption was NOT null or undefined
		r = t;
	}
	

	return r;
}

h.o = function(k){
	return $(k).data('o').o;
}


})(jQuery);
